package com.yongcheon.sms_manager.ui.phone_spam.list;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;

import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.data.phone_spsm.PhoneData;
import com.yongcheon.sms_manager.data.phone_spsm.SpamPhonData;
import com.yongcheon.sms_manager.data.preference.SpamNumberData;
import com.yongcheon.sms_manager.database.SpamDBManager;
import com.yongcheon.sms_manager.ui.dialogs.SpamRegistDlg;
import com.yongcheon.sms_manager.vo.ContactVO;
import com.yongcheon.sms_manager.vo.PhoneVO;
import com.yongcheon.sms_manager.vo.SpamNumVO;

import java.util.ArrayList;

/**
 * Created by yongcheon on 2014-11-27.
 */
public class PhoneLongClickListener implements View.OnLongClickListener{
    PhoneVO vo;
    ContactVO contactvo;
    View view;

    public PhoneLongClickListener(PhoneVO vo, ContactVO contactvo, View view){
        super();
        this.vo = vo;
        this.contactvo = contactvo;
        this.view = view;
    }

    @Override
    public boolean onLongClick(View v) {
        String[] MENU_LIST = {v.getContext().getResources().getString(R.string.delete_this_call_log), v.getContext().getResources().getString(R.string.delete_same_number_call),
//                v.getContext().getResources().getString(R.string.register_spam_one),
                              v.getContext().getResources().getString(R.string.register_spam_total)};

        String from = (contactvo != null) ? contactvo.getName() : vo.getNumber();
        AlertDialog dialog = new AlertDialog.Builder(v.getContext())
                .setTitle(from)
                .setItems(MENU_LIST, new PhoneDialogOnClickListener(v.getContext()))
                .create();

        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        return false;
    }

    public class PhoneDialogOnClickListener implements  DialogInterface.OnClickListener{
        private Context context;
        private PhoneData phoneData;
        private SpamPhonData spamPhonData;
        private SpamNumberData spamNumData;
        private SQLiteDatabase database;

        public PhoneDialogOnClickListener(Context context){
            super();
            this.context = context;
            this.phoneData = new PhoneData();
            this.database = SpamDBManager.getDatabase(context);
            this.spamNumData = new SpamNumberData(context);
            this.spamPhonData = new SpamPhonData();
        }

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
                case 0 :
                    phoneData.delete(PhoneLongClickListener.this.vo.getId(), context, null);
                break;
                case 1:
                    phoneData.deleteByPhoneNum(PhoneLongClickListener.this.vo.getNumber(), context,  null);
                break;
                case 2 :
                    SpamNumVO spamNumVO = getSpamNumInfo(PhoneLongClickListener.this.vo.getNumber());
                    if(spamNumVO != null){
                        registAllSpam(spamNumVO);
                    }else{
                        spamNumVO = new SpamNumVO();
                        spamNumVO.setAddress(PhoneLongClickListener.this.vo.getNumber());
                        spamNumVO.setCategory_1(context.getResources().getString(R.string.user_regist));
                        spamNumVO.setDesc("");
                        SpamRegistDlg.showSpamRegistDlg(context, view, spamNumVO, this, false);
                    }
                break;
            }
        }

        public SpamNumVO getSpamNumInfo(String address){
            SpamNumVO spamNumVO = null;
            ArrayList<SpamNumVO> list = spamNumData.selectReturnVo(address, context, database);
            if(list.size() > 0) spamNumVO = list.get(0);
            return spamNumVO;
        }
//
//        public void registOnlyOneSpam(SpamNumVO spamNumVO){
//            ArrayList<SMSVO> smsList = smsData.selectById(String.valueOf(PhoneLongClickListener.this.vo.get_Id()), context, null);
//            registSpam(smsList, spamNumVO);
//        }
//
        public void registAllSpam(SpamNumVO spamNumVO){
            ArrayList<PhoneVO> smsList = phoneData.select(PhoneLongClickListener.this.vo.getNumber(), context, null);
            registSpam(smsList, spamNumVO);
        }
//
        public void registSpam(ArrayList<PhoneVO> aList, SpamNumVO spamNumVO){
            try {
                database.beginTransaction();

                for (int i = 0; i < aList.size(); i++) {
                    PhoneVO vo = aList.get(i);
                    vo.setSpam_category(spamNumVO.getCategory_2());
                    vo.setSpam_desc(spamNumVO.getSubject());
                    spamPhonData.insert(aList.get(i), context, database);
                }

                spamNumData.insertNotExist(spamNumVO, context, database);

                for (int i = 0; i < aList.size(); i++) {
                    phoneData.delete(aList.get(i).getId(), context,  null);
                }

                database.setTransactionSuccessful();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                database.endTransaction();
            }
        }
//
    }
}
